from typing import List
from utils import parse_util as pu
from database.mysqlhelper import MySqLHelper

db = MySqLHelper()

insert_sql = 'insert into t_stock_monitor (f_code,f_name,f_user_id, f_total_hand, f_prompt_interval,f_note)  VALUES (%s, %s, %s, %s, %s, %s) '
select_str = 'select f_code,f_name,f_user_id, f_total_hand, f_prompt_interval,f_note, f_update_time from t_stock_monitor '
delete_str = 'delete from t_stock_monitor '


class StockMonitorDto:
    def __init__(self):
        self.code: str = ''
        self.name: str = ''
        self.user_id: str = ''
        self.total_hand: float = 0
        self.prompt_interval: float = 0
        self.note = ''  # 备注

    def __repr__(self):
        return repr((self.code, self.name, self.user_id, self.total_hand, self.prompt_interval, self.note))

    def to_tup(self) -> tuple:
        return (self.code, self.name, self.user_id, self.total_hand, self.prompt_interval, self.note)


# 格式化结果
def format_info(p):
    if not p:
        return
    smt = StockMonitorDto()
    smt.code = str(p[0], encoding="utf-8")
    smt.name = str(p[1], encoding="utf-8")
    smt.user_id = str(p[2], encoding="utf-8")
    smt.total_hand = float(p[3])
    smt.prompt_interval = float(p[4])
    smt.note = str(p[5], encoding="utf-8")
    return smt


# 格式化结果
def format_mul(res) -> List[StockMonitorDto]:
    r_list = []
    for p in res:
        r_list.append(format_info(p))
    return r_list


# 初始化所有股票
def insert_mul(data_list: List[StockMonitorDto]):
    d_len = len(data_list)
    if d_len == 0:
        print('t_stock_monitor插入的数据为0')
        return 0
    # 增加多条
    li = []
    for smd in data_list:
        li.append(smd.to_tup())
    return db.insertmany(insert_sql, li)


def insert_one(smd: StockMonitorDto):
    return db.insertone(insert_sql, smd.to_tup())


# 更新持仓
def update_monitor_hold(user_id, code, hold):
    sql = 'update t_stock_monitor set f_total_hand = %s where f_user_id = %s and f_code = %s '
    return db.update(sql, (hold, user_id, code))


# 更新备注
def update_monitor_note(user_id, code, note):
    sql = 'update t_stock_monitor set f_note = %s where f_user_id = %s and f_code = %s '
    return db.update(sql, (note, user_id, code))


# 删除监控
def delete_monitor(user_id, code):
    sql = delete_str + ' where f_user_id = %s and f_code = %s '
    return db.delete(sql, (user_id, code))


# 用户监控
def select_all(user_id):
    sql = select_str + ' where f_user_id = %s '
    res = db.selectall(sql, (user_id))
    return format_res(res)


# 用户监控
def select_distinct():
    # 增加多条
    sql = 'select distinct f_code,f_name from t_stock_monitor '
    res = db.selectall(sql)
    m_list = []
    for p in res:
        m_list.append(
            {'code': str(p[0], encoding="utf-8"),
             'name': str(p[1], encoding="utf-8")})
    return m_list


# 格式化结果
def format_one(p):
    if not p:
        return {}
    return {'code': str(p[0], encoding="utf-8"), 'name': str(p[1], encoding="utf-8"),
            'user_id': str(p[2], encoding="utf-8"),
            'total_hand': float(p[3]), 'prompt_interval': float(p[4]), 'note': pu.byte_to_str(p[5])}


# 格式化结果
def format_res(res):
    r_list = []
    for p in res:
        r_list.append(format_one(p))
    return r_list


if __name__ == '__main__':
    select_all('t1qznXH')
